from django.db import models
from django.contrib.auth import get_user_model

User = get_user_model()


class Song(models.Model):
    title = models.CharField(max_length=128)
    artist = models.CharField(max_length=128)
    cover = models.ImageField(upload_to='songs/song_cover/', null=True, blank=True)
    audio = models.FileField(upload_to='songs/song_audio/')
    duration = models.PositiveIntegerField(help_text="单位为秒")  # 用整数避免 float 精度问题
    lyrics = models.TextField(blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-created_at']


class Playlist(models.Model):
    name = models.CharField(max_length=128)
    description = models.TextField(blank=True)
    creator = models.ForeignKey(User, on_delete=models.CASCADE)
    songs = models.ManyToManyField(Song, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-created_at']


class PlayHistory(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    song = models.ForeignKey(Song, on_delete=models.CASCADE)
    played_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-played_at']
